package com.dek.ssm.ch2.reflect;

import java.lang.reflect.Method;

public class ReflectServiceImpl {
	
	
	public void sayHello(String name) {
		System.err.println("hello " + name);
	}
	
	/**
	 *  反射生成对象
	 * @return
	 */
	public static ReflectServiceImpl getInstance() {
		ReflectServiceImpl object = null;
		try {
			object = (ReflectServiceImpl) Class.forName("com.dek.ssm.ch2.reflect.ReflectServiceImpl").newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	
	/**
	 * reflect method
	 * @return
	 */
	public Object reflectMethod() {
		Object object = null;
		ReflectServiceImpl target = ReflectServiceImpl.getInstance();
		try {
			Method method = ReflectServiceImpl.class.getMethod("sayHello", String.class);
			object = method.invoke(target, "dek");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	
	public Object reflect() {
		ReflectServiceImpl object = null;
		try {
			object = (ReflectServiceImpl) Class.forName ("com.dek.ssm.ch2.reflect.ReflectServiceImpl").
					newInstance();
			Method method = object.getClass().getMethod("sayHello", String.class);
			method.invoke(object, "dek");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	
	
	public static void main(String[] args) {
		ReflectServiceImpl.getInstance().sayHello("dek");
		ReflectServiceImpl.getInstance().reflectMethod();
		ReflectServiceImpl.getInstance().reflect();
	}
	
}
